package com.bprogram.controller;


import com.bprogram.pojo.Subscribe;
import com.bprogram.pojo.User;
import com.bprogram.service.SubscribeService;
import com.bprogram.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 订阅关注表 前端控制器
 * </p>
 *
 * @author cai
 * @since 2023-11-21
 */
@RestController
@RequestMapping("/subscribe")
public class SubscribeController {

    @Resource
    private SubscribeService subscribeService;

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private RedisUtil redisUtil;
    /**
     * 关注
     *
     * @param subscribe
     * @return
     */
    @PostMapping("/saveSubscribe")
    public boolean saveSubscribe(Subscribe subscribe) {
        return subscribeService.saveSubscribe(subscribe);
    }

    /**
     * 取消关注
     *
     * @param upId
     * @return
     */
    @PostMapping("/cancelSubscribe")
    public boolean cancelSubscribe(long upId) {
        return subscribeService.cancelSubscribe(upId);
    }

    /**
     * 获取两人的关联关系
     *
     * @param upId
     * @return
     */
    @GetMapping("/getSubscribeByUpId")
    public Subscribe getSubscribeByUpId(long upId) {
        return subscribeService.getSubscribeByUpId(upId);
    }


    /**
     * 获取当前登录人的关注列表或粉丝列表
     * @param type fans:粉丝列表， up:关注列表
     * @return
     */
    @GetMapping("/getUpsOrFans")
    public List<User> getUpsOrFans(String type){

        ArrayList<User> userList = new ArrayList<>();
//粉丝列表
        User loginUser = userFeign.getLoginUser();
        if("fansId".equals(type)){
            Set<Object> userIds = redisUtil.sGet(loginUser.getUserId() + "fansId");
            for (Object userId : userIds) {
                long id = (long) userId;
                User user = userFeign.getById(id);
                userList.add(user);
            }

        }
//关注列表
        if("fansId".equals(type)){
            Set<Object> userIds = redisUtil.sGet(loginUser.getUserId() + "upId");
            for (Object userId : userIds) {
                long id = (long) userId;
                User user = userFeign.getById(id);
                userList.add(user);
            }
        }
        return userList;
    }






}

